<template>
  <view></view>
</template>

<script>
import Taro from "@tarojs/taro";
import { setGlobalData, getGlobalData } from "../../IOParking/globalData.js";
export default {
  data() {
    return {
      code: "",//appid:oRPxj5ZrsZGRuxTa-UrzEBeLuv-s
      apiBaseUrl: "http://api-pre.customer.parking.keji.io/api/wxapp/v1", //请求头
      isScode: false,
      parkingCode: "",
      channelCode: "",
      mobile:'',
      openId:'',
    };
  },
  onLoad(option) {
    if (option.parkingCode) {
      //体验开发版本扫码获得参数
      this.isScode = true;
      this.parkingCode = option.parkingCode ? option.parkingCode : "";
      this.channelCode = option.channelCode ? option.channelCode : "";
    } else if (option.q) {
      //正式版扫码获得参数
      let qrUrl = decodeURIComponent(option.q);
      let parkingCode = this.getQueryString(qrUrl, "parkingCode");
      let channelCode = this.getQueryString(qrUrl, "channelCode");
      console.log("正式扫码参数:", qrUrl);
      console.log(
        "正式扫码parkingCode，channelCode:",
        parkingCode,
        channelCode
      );
      this.isScode = true;
      this.parkingCode = parkingCode ? parkingCode : "";
      this.channelCode = channelCode ? channelCode : "";
    } else if (option.parkingCode == "") {
      this.isScode = false;
    }
    this.wx_login();
  },
  created() {},
  methods: {
    openSubPack() {
      Taro.navigateTo({
        url: "/IOParking/pages/home/home" //修改成您的分包入口路径
      });
    },
    wx_login() {
      //微信登陆接口 获取code
      var that = this;
      Taro.login({
        success(res) {
          console.log("微信登陆接口 获取code", res);
          if (res.code) {
            //获取 sessionId openId
            that.code = res.code;
            that.getSessionId();
          }
        }
      });
    },
    getSessionId() {
      //后台登陆获取 sessionId openId unionId
      Taro.showLoading({
        title: "加载中"
      });
      var base = this.apiBaseUrl;
      var that = this;
      Taro.request({
        url:
          base +
          "/weixin/login?code=" +
          that.code +
          "&appid=wx5f73ec239822208c",
        data: {
          code: that.code,
          appid: "wx5f73ec239822208c" //小程序唯一ID
        },
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "GET",
        success(res) {
          console.log("后台登陆获取:", res.data);
          if (res.data.data.openId) {
            Taro.hideLoading()
            setGlobalData("openId", res.data.data.openId);
            that.qrCodeUrl();
          }
        }
      });
    },
    //跳转链接判断
    qrCodeUrl() {
      var that = this;
      if (that.isScode) {
        console.log("判断参数页面跳转：", that.parkingCode, that.channelCode);
        if (that.channelCode !== "") {
          console.log("通道扫码");
          Taro.redirectTo({
            url:
              "/IOParking/pages/trafficManage/goPay?parkingCode=" +
              that.parkingCode +
              "&channelCode=" +
              that.channelCode
          });
        } else {
          console.log("场内扫码");
          Taro.redirectTo({
            url: "/IOParking/pages/home/home?parkingCode=" + that.parkingCode
          });
        }
      } else {
        Taro.redirectTo({
          url: "/IOParking/pages/home/home"
        });
      }
    },
    //解析链接方法
    getQueryString(url, name) {
      var reg = new RegExp("(^|&|/?)" + name + "=([^&|/?]*)(&|/?|$)", "i");
      var r = url.substr(1).match(reg);
      if (r != null) {
        return r[2];
      }
      return null;
    }
  }
};
</script>